
/*-----------------------------------------------------------------
 *  STRING PROTOTYPE EXTENDS
 *-----------------------------------------------------------------*/
/**
 * Returns a capitalized version of the string. Only the first
 * character of each word in the string are capitalized.
 * 
 * @return	a capitalized version of the string
 */
String.prototype.capitalize = function()
{ 
    return this.replace(/\w+/g, function(a)
	{
        return a.charAt( 0 ).toUpperCase() + a.substr( 1 ).toLowerCase();
    });
};
/**
 * Replaces the < and > characters by their HTML equivalent, in order
 * to display the HTML content on the page.
 * 
 * @return the html string escaped to display the html content
 */
String.prototype.escapeHTML = function()
{ 
    return this.replace( /</g, "&lt;" ).replace( />/g,"&gt;");
}
/**
 * Replaces the HTML equivalent of the < and > characters by these characters.
 *  
 * @return the unescaped html string
 */
String.prototype.unescapeHTML = function()
{ 
    return this.replace( /&lt;/g, "<" ).replace( /&gt;/g,">");
};

/*-----------------------------------------------------------------
 * 	GENERAL UTILITIES FUNCTIONS AND OBJECTS
 *-----------------------------------------------------------------*/
function $ ( key )
{
	return document.getElementById( key );
}

/**
 * Include the passed-in url as a new script file to load
 * in the current page. The <i>include</i> function don't
 * insert directly the code at the call place, but the file
 * is loaded directly after the file into which the call was
 * done.
 *  
 * @param {String} libraryName	url of the script file to include
 */
function include ( libraryName )
{
    document.write('<script type="text/javascript" src="'+libraryName+'"><\/script>');
}
/**
 * Creates a new function which wrap the call to <i>thatMethod</i>
 * in the scope of <i>that</i> object. Aditional arguments can be
 * passed to <i>thatMethod</i>.
 * 
 * @param {Object} 	 that		scope object into which call the function
 * @param {Function} thatMethod	method to call in the specified scope
 * @param {Object} 	 ...args	all additional arguments are appended to 
 * 								the delegate call arguments.
 */
function delegate( that, thatMethod )
{
	var a = [];
	for (var i = 2; i < arguments.length; i++) 
	{
		a.push( arguments[i] );
	}
	return function() {	return thatMethod.apply( that, Array(arguments).concat( a ) ); }
}

/**
 *
 */
IdFactory = { id:0 };

/**
 *
 *
 * @return	a new unique id
 */
IdFactory.getNextId = function()
{
	return ++IdFactory.id;
}
/**
 *
 *
 * @return	the next id returned by the getNextId()
 */
IdFactory.previewNextId = function()
{
	return IdFactory.id + 1;
}

/*-----------------------------------------------------------------
 * DEBUG UTILITIES
 *-----------------------------------------------------------------*/
/**
 * Creates an alert with the content of the passed-in object.
 * 
 * @param {Object} o
 */
function printObj ( o )
{
	var s = "print " + o + " (" + typeof(o) + ")";
	for( var i in o )
	{
		s += "\n" + i + " : " + o[ i ];
	}
	alert( s );
}
/**
 * Creates an alert with the content of the passed-in array.
 * 
 * @param {Array} a
 */
function printArray ( a )
{
	var s = "print " + a + " [" + a.length + "]";
	var l = a.length;
	for( var i=0;i<l;i++ )
	{
		s += "\n" + i + " : " + a[ i ];
	}
	alert( s );
}
/*-----------------------------------------------------------------
 * 	STYLES UTILITIES
 *-----------------------------------------------------------------*/

/**
 * 
 * 
 * @param	{HTMLElement} el			element onto which look for the class name
 * @param	{String}	  classNames	class name string to match
 */
function hasClass ( el, className )
{
	return el.className.match( className );
}
/**
 * Appends the passed-in 'className' at the end of the className
 * property of the passed-in element el.
 *
 * @param	{HTMLElement} el			element onto which add the class name
 * @param	{String}	  classNames	class name string to add
 */
function addClass ( el, className )
{
	if( !hasClass ( el, className ) )
	{
		el.className += ' ' + className;
	}
}
/**
 * Removes the passed-in 'className' from the className of the passed-in
 * element el.
 *
 * @param	{HTMLElement} el			element onto which remove the class name
 * @param	{String}	  classNames	class name string to remove
 */
function removeClass ( el, className )
{
	if( hasClass ( el, className ) )
	{
		el.className = el.className.replace ( className, '');
	}
}
/**
 * Replaces the passed-in 'match' class name by the 'replace' one in
 * the className property of the passed-in element el.
 *
 * @param	{HTMLElement} el		element onto which replace class names
 * @param	{String}	  match		class name string to match
 * @param	{String}	  replace	class name string to replace by
 */
function replaceClass ( el, match, replace )
{
	if( hasClass ( el, match ) )
	{
		el.className = el.className.replace ( match, replace );
	}
}


